使用statefulset创建redis的master/slave replication模式

23次阅读
没有评论
使用 statefulset 创建 redis 的 master/slave replication 模式

因为 redis 的主从复制时,master 和 slave 角色的配置并不相同

因此采用了两个 statefulset 的方式去创建

 

创建 headless 的 service 名称

 

 [root@node1 2stateful]# cat headless.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  namespace: redis-test
spec:
  clusterIP: None  # Headless Service
  selector:
    app: redis-master
  ports:
    - port: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  namespace: redis-test
spec:
  clusterIP: None  # Headless Service
  selector:
    app: redis-slave
  ports:
    - port: 6379

 

 

对于 statefulset1.yaml 的配置


[root@node1 2stateful]# cat stateful1.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-master
  namespace: redis-test
spec:
  serviceName: redis-master  # Headless Service name
  replicas: 1
  selector:
    matchLabels:
      app: redis-master
  template:
    metadata:
      labels:
        app: redis-master
    spec:
      containers:
        - name: redis
          image: redis:latest
          ports:
            - containerPort: 6379
          volumeMounts:
            - name: redis-storage
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: redis-storage
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi

 

 

对于 statefulset2.yaml 的配置方式

[root@node1 2stateful]# cat stateful2.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-slave
  namespace: redis-test
spec:
  serviceName: redis-slave  # Headless Service name
  replicas: 1
  selector:
    matchLabels:
      app: redis-slave
  template:
    metadata:
      labels:
        app: redis-slave
    spec:
      containers:
        - name: redis
          image: redis:latest
          ports:
            - containerPort: 6379
          command: ["redis-server", "--slaveof", "redis-master-0.redis-master.redis-test.svc.cluster.local", "6379"]
          volumeMounts:
            - name: redis-storage
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: redis-storage
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
正文完
 
评论(没有评论)
验证码